//
//  LMBaseCollectionView.m
//  LMLabelSelect
//
//  Created by LM on 2022/11/30.
//

#import "LMBaseCollectionView.h"

@implementation LMBaseCollectionView
- (instancetype)initWithFrame:(CGRect)frame collectionViewLayout:(UICollectionViewLayout *)layout
{
    if (self = [super initWithFrame:frame collectionViewLayout:layout]) {
        
        _flowLayout = (UICollectionViewFlowLayout *)layout;
        
        self.delegate = self;
        
        self.dataSource = self;
    }
    return self;
}
#pragma mark - UICollectionViewDataSource
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section
{
    if (_numberOfItemsBlock) {
        
        return _numberOfItemsBlock(collectionView, section);
    }
    return 1;
}
- (NSInteger)numberOfSectionsInCollectionView:(UICollectionView *)collectionView
{
    if (_numberOfSectionsBlock) {
        
        return _numberOfSectionsBlock(collectionView);
    }
    return 1;
}
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_cellForItemsBlock) {
        
        return _cellForItemsBlock(collectionView, indexPath);
    }
    UICollectionViewCell *cell = [[UICollectionViewCell alloc]init];
    return cell;
}
- (UICollectionReusableView *)collectionView:(UICollectionView *)collectionView viewForSupplementaryElementOfKind:(NSString *)kind atIndexPath:(NSIndexPath *)indexPath
{
    if (_viewForSupplementaryElementBlock) {
        
        return _viewForSupplementaryElementBlock(collectionView, kind, indexPath);
    }
    UICollectionReusableView *view = [UICollectionReusableView new];
    return view;
}
#pragma mark - UICollectionViewDelegate
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_didSelectItemBlock) {
        
        _didSelectItemBlock(collectionView, indexPath);
        
    } else {
        
        [collectionView deselectItemAtIndexPath:indexPath animated:YES];
    }
}
- (void)collectionView:(UICollectionView *)collectionView didDeselectItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_didDeselectItemBlock) {
        
        _didDeselectItemBlock(collectionView, indexPath);
    }
}
#pragma mark - UICollectionViewDelegateFlowLayout
- (UIColor *)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout colorForSectionAtIndex:(NSInteger)section
{
    return [UIColor whiteColor];
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath
{
    if (_sizeForItemBlock) {
        
        return _sizeForItemBlock(collectionView, collectionViewLayout, indexPath);
    }
    return _flowLayout.itemSize;
}
- (UIEdgeInsets)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout insetForSectionAtIndex:(NSInteger)section
{
    if (_insetForSectionBlock) {
        
        return _insetForSectionBlock(collectionView, collectionViewLayout, section);
    }
    
    return _flowLayout.sectionInset;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumLineSpacingForSectionAtIndex:(NSInteger)section
{
    if (_minimulineSpacingBlock) {
        
        return _minimulineSpacingBlock(collectionView, collectionViewLayout, section);
    }
    return _flowLayout.minimumLineSpacing;
}
- (CGFloat)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout minimumInteritemSpacingForSectionAtIndex:(NSInteger)section
{
    if (_minimumInterItemSpacingBlock) {
        
        return _minimumInterItemSpacingBlock(collectionView, collectionViewLayout, section);
    }
    return _flowLayout.minimumInteritemSpacing;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForHeaderInSection:(NSInteger)section
{
    if (_refernceSizeForHeaderBlock) {
        
        return _refernceSizeForHeaderBlock(collectionView, collectionViewLayout, section);
    }
    return _flowLayout.headerReferenceSize;
}
- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout*)collectionViewLayout referenceSizeForFooterInSection:(NSInteger)section
{
    if (_refernceSizeForFooterBlock) {
        
        return _refernceSizeForFooterBlock(collectionView, collectionViewLayout, section);
    }
    return _flowLayout.footerReferenceSize;
}
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    if (_scrollViewDidScroll) {
        
        _scrollViewDidScroll(scrollView);
    }
}
- (void)scrollViewDidEndDecelerating:(UIScrollView *)scrollView
{
    if (_scrollViewDidEndDecelerating) {
        
        _scrollViewDidEndDecelerating(scrollView);
    }
}
- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    if (_scrollViewDidEndDragging) {
        _scrollViewDidEndDragging(scrollView,decelerate);
    }
}
- (void)scrollViewWillBeginDecelerating:(UIScrollView *)scrollView
{
    if (_scrollViewWillBeginDecelerating) {
        
        _scrollViewWillBeginDecelerating(scrollView);
    }
}
- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    if (_scrollViewWillBeginDragging) {
        
        _scrollViewWillBeginDragging(scrollView);
    }
}
- (void)setFlowLayout:(UICollectionViewFlowLayout *)flowLayout
{
    _flowLayout = flowLayout;
//    [self setCollectionViewLayout:flowLayout];
    [self reloadData];
}
@end
